package com.easpeed.elevator.ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
public class IntensityView extends View {
    private Bitmap bitmap;
    private String distanceText = ""; // 用于存储距离文本

    public IntensityView(Context context) {
        super(context);
    }

    public IntensityView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setBitmap(Bitmap bitmap, String distance) {
        this.bitmap = bitmap;
        this.distanceText = distance; // 更新距离文本
        invalidate(); // 请求重绘
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (bitmap != null) {
            int canvasWidth = getWidth();
            int canvasHeight = getHeight();
            Rect destRect = new Rect(0, 0, canvasWidth, canvasHeight);
            canvas.drawBitmap(bitmap, null, destRect, null);
        }

        // 在右下角绘制文本
        if (!distanceText.isEmpty()) {
            Paint textPaint = new Paint();
            textPaint.setColor(Color.BLACK); // 设置文本颜色
            textPaint.setTextSize(38); // 设置文本大小
            textPaint.setAntiAlias(true); // 反锯齿处理

            float textWidth1 = textPaint.measureText("选中距离：");
            float x1 = getWidth() - textWidth1 - 20; // 右侧留20像素边距
            float y1 = getHeight() - textPaint.getTextSize() *2 ; // 底部留40像素边距

            canvas.drawText("选中距离：", x1, y1, textPaint); // 绘制文本
            // 计算文本的位置
            float textWidth = textPaint.measureText(distanceText);
            float x = getWidth() - textWidth - 20; // 右侧留20像素边距
            float y = getHeight() - textPaint.getTextSize(); // 底部留20像素边距

            canvas.drawText(distanceText, x, y, textPaint); // 绘制文本
        }
    }
}
